using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Ability : LoadableJSONObject{

	public bool iterative = false;
	public string iterativeName;
	
	public bool replacementAbility = false;
	
	public List<string> replacementList;
	
	public Ability selectedReplacement;
	
	public string type
	{
		get
		{
			if(FeatManager.index.ContainsKey(name))
				return "Feat";
			return "SpecialAbility";
		}
	}
	
	public List<BuffDebuff> buffs; 
	
	public string getIteratedName(CharacterSheet sheet = null)
	{
		if(sheet == null)
			return parseIterativeName(1);
			
		List<Ability> all;
		
		if(type == "Feat")
			all = sheet.Feats.FindAll(delegate(Ability obj) {
				return obj.name == name;	
			});
		else
			all = sheet.specialAbility.FindAll(delegate(Ability obj) {
				return obj.name == name;	
			});
		
		return parseIterativeName(all.Count);
	}
	
	private string parseIterativeName(int x)
	{
		string[] parts = iterativeName.Split('{');
		if(parts.Length == 1 && x > 1)
			return iterativeName + " x" + x;
		else if(parts.Length == 1)
			return iterativeName;
		
		string reconstructed = parts[0];
		for(int i = 1; i < parts.Length; i++)
		{
			string[] subParts = (parts[i]).Split('}');
			reconstructed = reconstructed + HelperFunctions.Evaluate( (subParts[0]).Replace("x", x.ToString())) + subParts[1];
		}
		
		return reconstructed;
	}
	
	public Ability()
	{
		
	}
	
	public override void Load(string json, string file)
	{
		fileName = file;
		if(json != null)
		{
			Dictionary<string,object> data = MiniJSON.Json.Deserialize(json) as Dictionary<string,object>;
			
			iterative = bool.Parse(data["iterative"] as string);
			replacementAbility = bool.Parse(data["replacementAbility"] as string);
			name = data["name"] as string;
			description = data["description"] as string;
			iterativeName = data["iterativeName"] as string;
			
			if(replacementAbility)
			{
				replacementList = new List<string>();
				List<string> abN = (MiniJSON.Json.Deserialize(data["replacementList"] as string) as List<object>).ConvertAll<string>(o => o as string);
				abN.ForEach(n =>
				{
					if((FeatManager.index.ContainsKey(n) || SpecialAbilityManager.index.ContainsKey(n)))
						replacementList.Add(n);
				});
				
				if(data.ContainsKey("selectedReplacement"))
				{
					selectedReplacement = new Ability();
					selectedReplacement.Load(data["selectedReplacement"] as string, "");
				}
			}
			
			buffs = new List<BuffDebuff>();
			if(data.ContainsKey("buffs"))
			{
				List<string> objectsNames = (MiniJSON.Json.Deserialize(data["buffs"] as string) as List<object>).ConvertAll<string>(o => o as string);
				foreach(string objName in objectsNames)
				{
					buffs.Add(BuffDebuffManager.Load(objName));
				}
			}
		}
	}
	
	public override string ToJSON()
	{
		Dictionary<string,string> data = new Dictionary<string, string>();
		data.Add("iterative", iterative.ToString());
		data.Add("replacementAbility", replacementAbility.ToString());
		data.Add("name", name);
		data.Add("description", description);
		data.Add("builtIn", builtIn.ToString());
		
		if(replacementAbility)
			data.Add("replacementList", MiniJSON.Json.Serialize(replacementList));
		
		data.Add("iterativeName", iterativeName);
		
		if(buffs == null)
			buffs = new List<BuffDebuff>();
		
		List<string> buffsNames = buffs.ConvertAll<string>(b => b.name);
		
		data.Add("buffs", MiniJSON.Json.Serialize(buffsNames));
		
		if(selectedReplacement != null)
			data.Add("selectedReplacement", selectedReplacement.ToJSON());
		
		return MiniJSON.Json.Serialize(data);
	}
	
	public Ability DeepCopy()
	{
		Ability a = new Ability();
		a.Load(ToJSON(), fileName);
		return a;
	}
}
